<!DOCTYPE html>

<html lang="zh-CN">

<head>
    
    <title>图像期末指南 - 塔纳托斯</title>
    <meta charset="UTF-8">
    <meta name="keywords" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=5">
    
    

    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
    <meta name="description" content="Matlab图像基础% 读图片 % 方法1   &#39;路径&#39; im&#x3D;imread(&#39;路径&#39;); % 方法2 [filename,filepath]&#x3D;uigetfile(&#39;*.*&#39;); im&#x3D;imread([filepath,filename]); --------------------------------------------- % 彩转灰，3为彩，2为灰 if numel(size(im))">
<meta property="og:type" content="article">
<meta property="og:title" content="图像期末指南">
<meta property="og:url" content="http://hai-chao.gitee.io/blog/2021/06/26/19.tu-xiang-qi-mo-zhi-nan/index.html">
<meta property="og:site_name" content="塔纳托斯">
<meta property="og:description" content="Matlab图像基础% 读图片 % 方法1   &#39;路径&#39; im&#x3D;imread(&#39;路径&#39;); % 方法2 [filename,filepath]&#x3D;uigetfile(&#39;*.*&#39;); im&#x3D;imread([filepath,filename]); --------------------------------------------- % 彩转灰，3为彩，2为灰 if numel(size(im))">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-06-26T08:48:07.000Z">
<meta property="article:modified_time" content="2021-06-26T14:17:55.311Z">
<meta property="article:author" content="塔纳托斯">
<meta property="article:tag" content="总结">
<meta name="twitter:card" content="summary">
    <link rel="stylesheet" href="/blog/lib/jquery.fancybox.min.css?v=1679297521143">
    
    <link rel="stylesheet" href="/blog/lib/mdui_043tiny/css/mdui.css?v=1679297521143">
    <link rel="stylesheet" href="/blog/lib/iconfont/iconfont.css?v=1679297521143">
    <link rel="stylesheet" href="/blog/css/style.css?v=1679297521143">
     
    
<meta name="generator" content="Hexo 5.3.0"><link rel="alternate" href="/blog/atom.xml" title="塔纳托斯" type="application/atom+xml">
<link rel="stylesheet" href="/blog/css/prism-tomorrow.css" type="text/css"></head>

<body class="mdui-drawer-body-left">
    
    <div id="nexmoe-background">
        <div class="nexmoe-bg" style="background-image: url(https://cdn.jsdelivr.net/gh/nexmoe/nexmoe.github.io@latest/images/cover/5c3aec85a4343.jpg)"></div>
        <div class="mdui-appbar mdui-shadow-0">
            <div class="mdui-toolbar">
                <a mdui-drawer="{target: '#drawer', swipe: true}" title="menu" class="mdui-btn mdui-btn-icon mdui-ripple"><i class="mdui-icon nexmoefont icon-menu"></i></a>
                <div class="mdui-toolbar-spacer"></div>
                <!--<a href="javascript:;" class="mdui-btn mdui-btn-icon"><i class="mdui-icon material-icons">search</i></a>-->
                <a href="/blog/" title="塔纳托斯" class="mdui-btn mdui-btn-icon"><img src="https://s6.jpg.cm/2022/01/17/LFpniO.md.jpg" alt="塔纳托斯"></a>
            </div>
        </div>
    </div>
    <div id="nexmoe-header">
        <div class="nexmoe-drawer mdui-drawer" id="drawer">
    <div class="nexmoe-avatar mdui-ripple">
        <a href="/blog/" title="塔纳托斯">
            <img src="https://s6.jpg.cm/2022/01/17/LFpniO.md.jpg" alt="塔纳托斯" alt="塔纳托斯">
        </a>
    </div>
    <div class="nexmoe-count">
        <div><span>文章</span>29</div>
        <div><span>标签</span>6</div>
        <div><span>分类</span>0</div>
    </div>
    <div class="nexmoe-list mdui-list" mdui-collapse="{accordion: true}">
        
        <a class="nexmoe-list-item mdui-list-item mdui-ripple false" href="/blog/" title="文章归档">
            <i class="mdui-list-item-icon nexmoefont icon-container"></i>
            <div class="mdui-list-item-content">
                文章归档
            </div>
        </a>
        
    </div>
    <aside id="nexmoe-sidebar">
    
    
  <div class="nexmoe-widget-wrap">
    <div id="randomtagcloud" class="nexmoe-widget tagcloud nexmoe-rainbow">
      <a href="/blog/tags/java/" style="font-size: 10px;">java</a> <a href="/blog/tags/%E5%88%86%E4%BA%AB/" style="font-size: 20px;">分享</a> <a href="/blog/tags/%E5%90%83%E7%93%9C/" style="font-size: 10px;">吃瓜</a> <a href="/blog/tags/%E5%AD%A6%E4%B9%A0/" style="font-size: 16.67px;">学习</a> <a href="/blog/tags/%E6%80%BB%E7%BB%93/" style="font-size: 13.33px;">总结</a> <a href="/blog/tags/%E6%B8%B8%E5%90%8E%E6%84%9F/" style="font-size: 10px;">游后感</a>
    </div>
    
  </div>

    
</aside>
    <div class="nexmoe-copyright">
        &copy; 2023 塔纳托斯
        Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
        & <a href="https://github.com/theme-nexmoe/hexo-theme-nexmoe" target="_blank">Nexmoe</a>
        
    </div>
</div><!-- .nexmoe-drawer -->
    </div>
    <div id="nexmoe-content">
        <div class="nexmoe-primary">
            <div class="nexmoe-post">

  <article>
      
          <div class="nexmoe-post-cover"> 
              
              <h1>图像期末指南</h1>
          </div>
      
      
      <div class="nexmoe-post-meta nexmoe-rainbow" style="margin:10px 0!important;">
    <a><i class="nexmoefont icon-calendar-fill"></i>2021年06月26日</a>
</div>

      

      <h2 id="Matlab图像基础"><a href="#Matlab图像基础" class="headerlink" title="Matlab图像基础"></a>Matlab图像基础</h2><pre class=" language-matlab"><code class="language-matlab"><span class="token comment" spellcheck="true">% 读图片</span>
<span class="token comment" spellcheck="true">% 方法1   </span><span class="token string">'路径'</span>
im<span class="token operator">=</span><span class="token function">imread</span><span class="token punctuation">(</span><span class="token string">'路径'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">% 方法2</span>
<span class="token punctuation">[</span>filename<span class="token punctuation">,</span>filepath<span class="token punctuation">]</span><span class="token operator">=</span><span class="token function">uigetfile</span><span class="token punctuation">(</span><span class="token string">'*.*'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
im<span class="token operator">=</span><span class="token function">imread</span><span class="token punctuation">(</span><span class="token punctuation">[</span>filepath<span class="token punctuation">,</span>filename<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span>
<span class="token comment" spellcheck="true">% 彩转灰，3为彩，2为灰</span>
<span class="token keyword">if</span> <span class="token function">numel</span><span class="token punctuation">(</span><span class="token function">size</span><span class="token punctuation">(</span>im<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">==</span><span class="token number">3</span>
    im<span class="token operator">=</span><span class="token function">rgb2gray</span><span class="token punctuation">(</span>im<span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment" spellcheck="true">%方法1</span>
    im<span class="token operator">=</span><span class="token function">im</span><span class="token punctuation">(</span><span class="token operator">:</span><span class="token punctuation">,</span><span class="token operator">:</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>      <span class="token comment" spellcheck="true">%方法2，通过提取R分量</span>
<span class="token keyword">end</span>
<span class="token comment" spellcheck="true">% 彩转灰的另一种判断方法,3为彩，2为灰</span>
<span class="token keyword">if</span> <span class="token function">ndims</span><span class="token punctuation">(</span>im<span class="token punctuation">)</span><span class="token operator">==</span><span class="token number">3</span>
<span class="token keyword">end</span>
<span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span>
<span class="token comment" spellcheck="true">% 写图片，即保存到文件中</span>
<span class="token function">imwirte</span><span class="token punctuation">(</span>im<span class="token punctuation">,</span><span class="token string">'e:/1.png'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span>
<span class="token comment" spellcheck="true">% 添加噪声的函数 imnoise 具体使用看帮助文档</span>
<span class="token comment" spellcheck="true">% </span><span class="token string">''</span>中可以填入  gaussian  salt <span class="token operator">&amp;</span> pepper  等等
<span class="token comment" spellcheck="true">% 注意imnoise处理图片的灰度值的类型为uint8或0~1的double型</span>
im<span class="token operator">=</span><span class="token function">imnoise</span><span class="token punctuation">(</span>im<span class="token punctuation">,</span><span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span>
<span class="token function">exp</span><span class="token punctuation">(</span><span class="token punctuation">)</span>   <span class="token comment" spellcheck="true">% 指数函数</span>
<span class="token function">log</span><span class="token punctuation">(</span><span class="token punctuation">)</span>   <span class="token comment" spellcheck="true">% 自然对数ln</span>
<span class="token function">log2</span><span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token comment" spellcheck="true">% 以二为底</span>
<span class="token function">log10</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment" spellcheck="true">% 以十为底</span>
<span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span>
<span class="token function">double</span><span class="token punctuation">(</span><span class="token punctuation">)</span>    与 <span class="token function">im2double</span><span class="token punctuation">(</span><span class="token punctuation">)</span> 的区别
<span class="token function">double</span><span class="token punctuation">(</span><span class="token punctuation">)</span>    会把图像的灰度值从uint8转为double型
<span class="token function">im2double</span><span class="token punctuation">(</span><span class="token punctuation">)</span> 对图像做的是归一化，会把图像的灰度值除以<span class="token number">255</span>，在把图像的灰度值从uint8转为double型，因此图像此时的灰度值范围为<span class="token number">0</span><span class="token operator">~</span><span class="token number">1</span>

数据类型为double时，imshow的处理范围是<span class="token number">0</span><span class="token operator">~</span><span class="token number">1</span>
数据类型为uint8时，imshow的处理范围是<span class="token number">0</span><span class="token operator">~</span><span class="token number">255</span>
因此，数据类型为double且范围是<span class="token number">0</span><span class="token operator">~</span><span class="token number">255</span>，用imshow来显示图片会把大于<span class="token number">1</span>的都当作<span class="token number">1</span>，因此为一片白
解决办法：
figure<span class="token punctuation">;</span><span class="token function">imshow</span><span class="token punctuation">(</span>im<span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
figure<span class="token punctuation">;</span><span class="token function">imshow</span><span class="token punctuation">(</span><span class="token function">uint8</span><span class="token punctuation">(</span>im<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
注<span class="token operator">:</span> 图像处理时用double，不会丢失精度，显示图片时用uint8型

<span class="token function">zeros</span><span class="token punctuation">(</span><span class="token punctuation">)</span> 构造值全为<span class="token number">0</span>的矩阵，注意类型为double
<span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span>
<span class="token function">max</span><span class="token punctuation">(</span><span class="token punctuation">)</span> 和 <span class="token function">min</span><span class="token punctuation">(</span><span class="token punctuation">)</span> 使用法
<span class="token comment" spellcheck="true">% 针对一维</span>
如 nums<span class="token operator">=</span><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
temp<span class="token operator">=</span><span class="token function">max</span><span class="token punctuation">(</span>nums<span class="token punctuation">)</span><span class="token punctuation">;</span>
temp的值为<span class="token number">3</span>
另一种语法
<span class="token punctuation">[</span>temp<span class="token punctuation">,</span>index<span class="token punctuation">]</span><span class="token operator">=</span><span class="token function">max</span><span class="token punctuation">(</span>nums<span class="token punctuation">)</span><span class="token punctuation">;</span>
temp的值为<span class="token number">3</span>，index的为<span class="token number">2</span>       <span class="token comment" spellcheck="true">% temp存储的为最大值，index存储的为下标</span>
<span class="token comment" spellcheck="true">% 针对多维，及矩阵,三个点的作用为在下一行接着写</span>
nums<span class="token operator">=</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">;</span><span class="token punctuation">...</span>
      <span class="token number">3</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">;</span><span class="token punctuation">...</span>
      <span class="token number">2</span><span class="token punctuation">,</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">1</span>
<span class="token punctuation">]</span><span class="token punctuation">;</span>
temp<span class="token operator">=</span><span class="token function">max</span><span class="token punctuation">(</span>nums<span class="token punctuation">)</span><span class="token punctuation">;</span>
temp的值为<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">]</span>           <span class="token comment" spellcheck="true">% 注意是每一列的最大值</span>
<span class="token punctuation">[</span>temp<span class="token punctuation">,</span>index<span class="token punctuation">]</span><span class="token operator">=</span><span class="token function">max</span><span class="token punctuation">(</span>nums<span class="token punctuation">)</span><span class="token punctuation">;</span>
temp的值为<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">,</span>index的值为<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">% min()的用法与之同理</span></code></pre>
<h2 id="图像增强"><a href="#图像增强" class="headerlink" title="图像增强"></a>图像增强</h2><h4 id="空域点运算"><a href="#空域点运算" class="headerlink" title="空域点运算"></a>空域点运算</h4><pre class=" language-matlab"><code class="language-matlab"><span class="token comment" spellcheck="true">% 点处理，作用于单个像素的空间域处理方法</span>
<span class="token comment" spellcheck="true">% 点运算是一种像素的逐点运算，是灰度到灰度的映射过程</span></code></pre>
<pre class=" language-matlab"><code class="language-matlab"><span class="token comment" spellcheck="true">% 灰度变化增强（线性点运算、分段线性点运算、非线性点运算）</span>
<span class="token comment" spellcheck="true">% 线性点运算，给出一个线性方程f(x)=ax+b，对每个像素进行该方程的处理,看下面的例子(***注意以下是针对灰度图像***)</span>
im<span class="token operator">=</span><span class="token function">imread</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
a<span class="token operator">=</span><span class="token number">1.2</span><span class="token punctuation">;</span>             <span class="token comment" spellcheck="true">% a>1,图像的对比度增加(图像变亮);0&lt;a&lt;1,图像对比度减少（图像变暗）</span>
b<span class="token operator">=</span><span class="token number">10</span><span class="token punctuation">;</span>
im<span class="token operator">=</span>a<span class="token operator">*</span>im<span class="token operator">+</span>b<span class="token punctuation">;</span>
figure<span class="token punctuation">;</span><span class="token function">imshow</span><span class="token punctuation">(</span>im<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span>
<span class="token comment" spellcheck="true">% 上述程序另一中写法</span>
im<span class="token operator">=</span><span class="token function">imread</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
a<span class="token operator">=</span><span class="token number">1.2</span><span class="token punctuation">;</span>
b<span class="token operator">=</span><span class="token number">10</span><span class="token punctuation">;</span>
<span class="token punctuation">[</span>row<span class="token punctuation">,</span>col<span class="token punctuation">]</span><span class="token operator">=</span><span class="token function">size</span><span class="token punctuation">(</span>im<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">for</span> <span class="token number">i</span><span class="token operator">=</span><span class="token number">1</span><span class="token operator">:</span>row
    <span class="token keyword">for</span> <span class="token number">j</span><span class="token operator">=</span><span class="token number">1</span><span class="token operator">:</span>col
        <span class="token function">im</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token operator">=</span>a<span class="token operator">*</span><span class="token function">im</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token operator">+</span>b<span class="token punctuation">;</span>
    <span class="token keyword">end</span>
<span class="token keyword">end</span>
figure<span class="token punctuation">;</span><span class="token function">imshow</span><span class="token punctuation">(</span>im<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">% 分段线性点运算,给出的方程为一个线性的分段的函数,例如0&lt;=灰度值&lt;150执行f1，150&lt;灰度值&lt;=200执行f2,200&lt;灰度值&lt;255执行f3</span>
im<span class="token operator">=</span><span class="token function">imread</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">[</span>row<span class="token punctuation">,</span>col<span class="token punctuation">]</span><span class="token operator">=</span><span class="token function">size</span><span class="token punctuation">(</span>im<span class="token punctuation">)</span><span class="token punctuation">;</span>
im<span class="token operator">=</span><span class="token function">im2double</span><span class="token punctuation">(</span>im<span class="token punctuation">)</span><span class="token punctuation">;</span>       <span class="token comment" spellcheck="true">% 这把图片的灰度值转为0~1，使运算更合理,精度不会丢失，得到图片的效果更好</span>
                        <span class="token comment" spellcheck="true">% 转为0~1后，下面的范围都要除去255</span>
res<span class="token operator">=</span><span class="token function">zeros</span><span class="token punctuation">(</span>row<span class="token punctuation">,</span>col<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">for</span> <span class="token number">i</span><span class="token operator">=</span><span class="token number">1</span><span class="token operator">:</span>row
    <span class="token keyword">for</span> <span class="token number">j</span><span class="token operator">=</span><span class="token number">1</span><span class="token operator">:</span>col
        <span class="token keyword">if</span> <span class="token number">0</span><span class="token operator">&lt;=</span><span class="token function">im</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token punctuation">)</span> <span class="token operator">&amp;</span> <span class="token function">im</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token punctuation">)</span> <span class="token operator">&lt;=</span> <span class="token number">150</span><span class="token operator">/</span><span class="token number">255</span>
            <span class="token function">res</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token number">1</span><span class="token operator">*</span><span class="token function">im</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token punctuation">;</span>                   <span class="token comment" spellcheck="true">%f1(x)=x</span>
        <span class="token keyword">elseif</span> <span class="token number">150</span><span class="token operator">/</span><span class="token number">255</span> <span class="token operator">&lt;</span> <span class="token function">im</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token punctuation">)</span> <span class="token operator">&amp;</span> <span class="token function">im</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token punctuation">)</span> <span class="token operator">&lt;=</span> <span class="token number">200</span><span class="token operator">/</span><span class="token number">255</span>
            <span class="token function">res</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token number">2</span><span class="token operator">*</span><span class="token function">im</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token number">0.5</span><span class="token punctuation">;</span>               <span class="token comment" spellcheck="true">%f2(x)=2x+0.5</span>
        <span class="token keyword">else</span>
            <span class="token function">res</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token operator">-</span><span class="token number">3</span><span class="token operator">*</span><span class="token function">im</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token number">9</span><span class="token punctuation">;</span>                <span class="token comment" spellcheck="true">%f3(x)=-3x+9</span>
        <span class="token keyword">end</span>
    <span class="token keyword">end</span>
<span class="token keyword">end</span>

figure<span class="token punctuation">;</span><span class="token function">imshow</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">% 非线性点运算，给出的方程为非线性的(都有通过循环的方式来做运算)</span>
<span class="token comment" spellcheck="true">% 反转变换</span>
im<span class="token operator">=</span><span class="token function">imread</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
im<span class="token operator">=</span><span class="token function">im2double</span><span class="token punctuation">(</span>im<span class="token punctuation">)</span><span class="token punctuation">;</span>
im<span class="token operator">=</span><span class="token number">1</span><span class="token operator">-</span>im<span class="token punctuation">;</span>
figure<span class="token punctuation">;</span><span class="token function">imshow</span><span class="token punctuation">(</span>im<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">% 对数变换</span>
im<span class="token operator">=</span><span class="token function">imread</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
im<span class="token operator">=</span><span class="token function">im2double</span><span class="token punctuation">(</span>im<span class="token punctuation">)</span><span class="token punctuation">;</span>
im<span class="token operator">=</span><span class="token number">0.5</span><span class="token operator">*</span><span class="token function">log</span><span class="token punctuation">(</span>im<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
figure<span class="token punctuation">;</span><span class="token function">imshow</span><span class="token punctuation">(</span>im<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">% 幂次变化</span>
im<span class="token operator">=</span><span class="token function">imread</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
im<span class="token operator">=</span><span class="token function">im2double</span><span class="token punctuation">(</span>im<span class="token punctuation">)</span><span class="token punctuation">;</span>
im<span class="token operator">=</span><span class="token number">0.2</span><span class="token operator">*</span><span class="token punctuation">(</span>im<span class="token operator">.^</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>            <span class="token comment" spellcheck="true">% 注意是.^</span>
figure<span class="token punctuation">;</span><span class="token function">imshow</span><span class="token punctuation">(</span>im<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span>
<span class="token comment" spellcheck="true">% 另一种写法</span>
im<span class="token operator">=</span><span class="token function">imread</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
im<span class="token operator">=</span><span class="token function">im2double</span><span class="token punctuation">(</span>im<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">[</span>row<span class="token punctuation">,</span>col<span class="token punctuation">]</span><span class="token operator">=</span><span class="token function">size</span><span class="token punctuation">(</span>im<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">for</span> <span class="token number">i</span><span class="token operator">=</span><span class="token number">1</span><span class="token operator">:</span>row
    <span class="token keyword">for</span> <span class="token number">j</span><span class="token operator">=</span><span class="token number">1</span><span class="token operator">:</span>col
        <span class="token function">im</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token number">0.2</span><span class="token operator">*</span><span class="token punctuation">(</span><span class="token function">im</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token operator">^</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>         <span class="token comment" spellcheck="true">% 注意针对到每个像素时，用^</span>
    <span class="token keyword">end</span>
<span class="token keyword">end</span>
figure<span class="token punctuation">;</span><span class="token function">imshow</span><span class="token punctuation">(</span>im<span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
<pre class=" language-matlab"><code class="language-matlab"><span class="token comment" spellcheck="true">% 基于直方图的图像增强法(直方图均衡化、直方图规定化)</span>
<span class="token comment" spellcheck="true">% 直方图均衡化</span>
步骤：
<span class="token number">1</span>、读入一张灰度图像
<span class="token number">2</span>、统计每个灰度的数目
<span class="token number">3</span>、计算概率
<span class="token number">4</span>、计算累积概率
<span class="token number">5</span>、确定映射
im<span class="token operator">=</span><span class="token function">imread</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">[</span>row<span class="token punctuation">,</span>col<span class="token punctuation">]</span><span class="token operator">=</span><span class="token function">size</span><span class="token punctuation">(</span>im<span class="token punctuation">)</span><span class="token punctuation">;</span>
figure<span class="token punctuation">;</span><span class="token function">imshow</span><span class="token punctuation">(</span>im<span class="token punctuation">)</span><span class="token punctuation">;</span>figure<span class="token punctuation">;</span><span class="token function">imhist</span><span class="token punctuation">(</span>im<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">% 统计每个灰度的数目</span>
count<span class="token operator">=</span><span class="token function">zeros</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">256</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">for</span> <span class="token number">i</span><span class="token operator">=</span><span class="token number">1</span><span class="token operator">:</span>row
    <span class="token keyword">for</span> <span class="token number">j</span><span class="token operator">=</span><span class="token number">1</span><span class="token operator">:</span>col
        <span class="token function">count</span><span class="token punctuation">(</span><span class="token function">im</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token function">count</span><span class="token punctuation">(</span><span class="token function">im</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token keyword">end</span>
<span class="token keyword">end</span>
<span class="token comment" spellcheck="true">% 计算概率</span>
p<span class="token operator">=</span><span class="token function">zeros</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">256</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">for</span> <span class="token number">i</span><span class="token operator">=</span><span class="token number">1</span><span class="token operator">:</span><span class="token number">256</span>
    <span class="token function">p</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token function">count</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">)</span><span class="token operator">/</span><span class="token punctuation">(</span>row<span class="token operator">*</span>col<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">end</span>
<span class="token comment" spellcheck="true">% 计算累积概率</span>
s<span class="token operator">=</span><span class="token function">zeros</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">256</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">for</span> <span class="token number">i</span><span class="token operator">=</span><span class="token number">1</span><span class="token operator">:</span><span class="token number">256</span>
    <span class="token keyword">if</span> <span class="token number">i</span><span class="token operator">==</span><span class="token number">1</span>
        <span class="token function">s</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token function">p</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">else</span>
        <span class="token function">s</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token function">s</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token function">p</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">end</span>
<span class="token keyword">end</span>
<span class="token comment" spellcheck="true">% 确定映射</span>
s<span class="token operator">=</span><span class="token function">uint8</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token operator">.*</span>s<span class="token operator">+</span><span class="token number">0.5</span><span class="token punctuation">)</span><span class="token punctuation">;</span>          <span class="token comment" spellcheck="true">%进行累积分布取整</span>
<span class="token keyword">for</span> <span class="token number">i</span><span class="token operator">=</span><span class="token number">1</span><span class="token operator">:</span>row
    <span class="token keyword">for</span> <span class="token number">j</span><span class="token operator">=</span><span class="token number">1</span><span class="token operator">:</span>col
        <span class="token function">im</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token function">s</span><span class="token punctuation">(</span><span class="token function">im</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">end</span>
<span class="token keyword">end</span>
figure<span class="token punctuation">;</span><span class="token function">imshow</span><span class="token punctuation">(</span>im<span class="token punctuation">)</span><span class="token punctuation">;</span>figure<span class="token punctuation">;</span><span class="token function">imhist</span><span class="token punctuation">(</span>im<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span>
<span class="token comment" spellcheck="true">% 直方图规定化</span>
<span class="token comment" spellcheck="true">% 规定化的实现有两种映射方式，单映射和组映射。这两者的区别在于映射的规则不同，之前的步骤大同小异。</span>
步骤：
<span class="token number">1</span>、读入原图像和标准图像
<span class="token number">2</span>、统计原图和标准图的灰度个数
<span class="token number">3</span>、计算两者的概率
<span class="token number">4</span>、计算累积概率
<span class="token number">5</span>、确定映射
<span class="token number">6</span>、根据映射得到新图片
单映射：
单映射为原图当前灰度级的累积概率减去标准图片的所有灰度级的累积概率，取其中差值的绝对值最小的组成映射关系
组映射：
组映射为标准图片的当前灰度级的累积概率（概率为<span class="token number">0</span>时跳向下一灰度级）减去原图的所有累积概率，取其中差值的绝对值最小的灰度级做一个标志，与之前的灰度级组成一个范围。重复这个过程
<span class="token comment" spellcheck="true">% 单映射</span>
<span class="token comment" spellcheck="true">% 读入原图像和标准图像</span>
im<span class="token operator">=</span><span class="token function">imread</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
im_st<span class="token operator">=</span><span class="token function">imread</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token punctuation">[</span>row<span class="token punctuation">,</span>col<span class="token punctuation">]</span><span class="token operator">=</span><span class="token function">size</span><span class="token punctuation">(</span>im<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">[</span>row_st<span class="token punctuation">,</span>col_st<span class="token punctuation">]</span><span class="token operator">=</span><span class="token function">size</span><span class="token punctuation">(</span>im_st<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">% 统计每个灰度的数目</span>
count<span class="token operator">=</span><span class="token function">zeros</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">256</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
count_st<span class="token operator">=</span><span class="token function">zeros</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">256</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">for</span> <span class="token number">i</span><span class="token operator">=</span><span class="token number">1</span><span class="token operator">:</span>row
    <span class="token keyword">for</span> <span class="token number">j</span><span class="token operator">=</span><span class="token number">1</span><span class="token operator">:</span>col
        <span class="token function">count</span><span class="token punctuation">(</span><span class="token function">im</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token function">count</span><span class="token punctuation">(</span><span class="token function">im</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token keyword">end</span>
<span class="token keyword">end</span>
<span class="token keyword">for</span> <span class="token number">i</span><span class="token operator">=</span><span class="token number">1</span><span class="token operator">:</span>row_st
    <span class="token keyword">for</span> <span class="token number">j</span><span class="token operator">=</span><span class="token number">1</span><span class="token operator">:</span>col_st
        <span class="token function">count_st</span><span class="token punctuation">(</span><span class="token function">im_st</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token function">count_st</span><span class="token punctuation">(</span><span class="token function">im_st</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token keyword">end</span>
<span class="token keyword">end</span>
<span class="token comment" spellcheck="true">% 计算分布概率（可以使用和上面直方图均衡化中的循环来做，这演示另一中不通过循环的做法）</span>
p<span class="token operator">=</span>count<span class="token operator">/</span><span class="token punctuation">(</span>row<span class="token operator">*</span>col<span class="token punctuation">)</span><span class="token punctuation">;</span>
p_st<span class="token operator">=</span>count_st<span class="token operator">/</span><span class="token punctuation">(</span>row_st<span class="token operator">*</span>col_st<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">% 计算累积概率</span>
s<span class="token operator">=</span><span class="token function">zeros</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">256</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
s_st<span class="token operator">=</span><span class="token function">zeros</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">256</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">for</span> <span class="token number">i</span><span class="token operator">=</span><span class="token number">1</span><span class="token operator">:</span><span class="token number">256</span>
    <span class="token keyword">if</span> <span class="token number">i</span><span class="token operator">==</span><span class="token number">1</span>
        <span class="token function">s</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token function">p</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">s_st</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token function">p_st</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">else</span>
        <span class="token function">s</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token function">s</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token function">p</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">s_st</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token function">s_st</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token function">p_st</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">end</span>
<span class="token keyword">end</span>
<span class="token comment" spellcheck="true">% 确定映射</span>
index<span class="token operator">=</span><span class="token function">zeros</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">256</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">for</span> <span class="token number">i</span><span class="token operator">=</span><span class="token number">1</span><span class="token operator">:</span><span class="token number">256</span>
    values<span class="token operator">=</span><span class="token function">zeros</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">256</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span> <span class="token number">j</span><span class="token operator">=</span><span class="token number">1</span><span class="token operator">:</span><span class="token number">256</span>
        <span class="token comment" spellcheck="true">% 注意：原图减去标准图</span>
        <span class="token function">values</span><span class="token punctuation">(</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token function">abs</span><span class="token punctuation">(</span><span class="token function">s</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token function">s_st</span><span class="token punctuation">(</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">end</span>
    <span class="token punctuation">[</span>temp<span class="token punctuation">,</span><span class="token function">index</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token function">min</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span><span class="token punctuation">;</span>              <span class="token comment" spellcheck="true">% 看min()的用法</span>
<span class="token keyword">end</span>
<span class="token comment" spellcheck="true">% 根据映射得到新图片</span>
res<span class="token operator">=</span><span class="token function">zeros</span><span class="token punctuation">(</span>row<span class="token punctuation">,</span>col<span class="token punctuation">)</span><span class="token punctuation">;</span>        <span class="token comment" spellcheck="true">%注意zeros生成的矩阵默认类型为double</span>
<span class="token keyword">for</span> <span class="token number">i</span><span class="token operator">=</span><span class="token number">1</span><span class="token operator">:</span>row
    <span class="token keyword">for</span> <span class="token number">j</span><span class="token operator">=</span><span class="token number">1</span><span class="token operator">:</span>col
        <span class="token function">res</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token function">index</span><span class="token punctuation">(</span><span class="token function">im_st</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>       <span class="token comment" spellcheck="true">% 先加1是把灰度值+1与数组下标相映射，后减1是把index中存的下标转为灰度</span>
    <span class="token keyword">end</span>
<span class="token keyword">end</span>
res<span class="token operator">=</span><span class="token function">uint8</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span><span class="token punctuation">;</span>
figure<span class="token punctuation">;</span><span class="token function">imshow</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span><span class="token punctuation">;</span>figure<span class="token punctuation">;</span><span class="token function">imhist</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span>
<span class="token comment" spellcheck="true">% 组映射</span>
<span class="token comment" spellcheck="true">% 读入原图像和标准图像</span>
im<span class="token operator">=</span><span class="token function">imread</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
im_st<span class="token operator">=</span><span class="token function">imread</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token punctuation">[</span>row<span class="token punctuation">,</span>col<span class="token punctuation">]</span><span class="token operator">=</span><span class="token function">size</span><span class="token punctuation">(</span>im<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">[</span>row_st<span class="token punctuation">,</span>col_st<span class="token punctuation">]</span><span class="token operator">=</span><span class="token function">size</span><span class="token punctuation">(</span>im_st<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">% 统计每个灰度的数目</span>
count<span class="token operator">=</span><span class="token function">zeros</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">256</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
count_st<span class="token operator">=</span><span class="token function">zeros</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">256</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">for</span> <span class="token number">i</span><span class="token operator">=</span><span class="token number">1</span><span class="token operator">:</span>row
    <span class="token keyword">for</span> <span class="token number">j</span><span class="token operator">=</span><span class="token number">1</span><span class="token operator">:</span>col
        <span class="token function">count</span><span class="token punctuation">(</span><span class="token function">im</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token function">count</span><span class="token punctuation">(</span><span class="token function">im</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token keyword">end</span>
<span class="token keyword">end</span>
<span class="token keyword">for</span> <span class="token number">i</span><span class="token operator">=</span><span class="token number">1</span><span class="token operator">:</span>row_st
    <span class="token keyword">for</span> <span class="token number">j</span><span class="token operator">=</span><span class="token number">1</span><span class="token operator">:</span>col_st
        <span class="token function">count_st</span><span class="token punctuation">(</span><span class="token function">im_st</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token function">count_st</span><span class="token punctuation">(</span><span class="token function">im_st</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token keyword">end</span>
<span class="token keyword">end</span>
<span class="token comment" spellcheck="true">% 计算分布概率</span>
p<span class="token operator">=</span>count<span class="token operator">/</span><span class="token punctuation">(</span>row<span class="token operator">*</span>col<span class="token punctuation">)</span><span class="token punctuation">;</span>
p_st<span class="token operator">=</span>count_st<span class="token operator">/</span><span class="token punctuation">(</span>row_st<span class="token operator">*</span>col_st<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">% 计算累积概率</span>
s<span class="token operator">=</span><span class="token function">zeros</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">256</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
s_st<span class="token operator">=</span><span class="token function">zeros</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">256</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">for</span> <span class="token number">i</span><span class="token operator">=</span><span class="token number">1</span><span class="token operator">:</span><span class="token number">256</span>
    <span class="token keyword">if</span> <span class="token number">i</span><span class="token operator">==</span><span class="token number">1</span>
        <span class="token function">s</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token function">p</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">s_st</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token function">p_st</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">else</span>
        <span class="token function">s</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token function">s</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token function">p</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">s_st</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token function">s_st</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token function">p_st</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">end</span>
<span class="token keyword">end</span>
<span class="token comment" spellcheck="true">% 确定映射</span>
end1<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>
end2<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>
index<span class="token operator">=</span><span class="token function">zeros</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">256</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">for</span> <span class="token number">i</span><span class="token operator">=</span><span class="token number">1</span><span class="token operator">:</span><span class="token number">256</span>
    values<span class="token operator">=</span><span class="token function">zeros</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">256</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token function">s_st</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">)</span><span class="token operator">==</span><span class="token number">0</span>
        <span class="token keyword">continue</span><span class="token punctuation">;</span>
    <span class="token keyword">else</span>
        <span class="token keyword">for</span> <span class="token number">j</span><span class="token operator">=</span><span class="token number">1</span><span class="token operator">:</span><span class="token number">256</span>
            <span class="token comment" spellcheck="true">% 标准图减去原图</span>
            <span class="token function">values</span><span class="token punctuation">(</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token function">abs</span><span class="token punctuation">(</span><span class="token function">s_st</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token function">s</span><span class="token punctuation">(</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">end</span>
    <span class="token keyword">end</span>
    <span class="token punctuation">[</span>temp<span class="token punctuation">,</span>end2<span class="token punctuation">]</span><span class="token operator">=</span><span class="token function">min</span><span class="token punctuation">(</span>values<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span> <span class="token number">j</span><span class="token operator">=</span>end1<span class="token operator">:</span>end2
        <span class="token function">index</span><span class="token punctuation">(</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token number">i</span><span class="token punctuation">;</span>
    <span class="token keyword">end</span>
    end1<span class="token operator">=</span>end2<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">;</span>
<span class="token keyword">end</span>
<span class="token comment" spellcheck="true">% 根据映射得到新图片</span>
res<span class="token operator">=</span><span class="token function">zeros</span><span class="token punctuation">(</span>row<span class="token punctuation">,</span>col<span class="token punctuation">)</span><span class="token punctuation">;</span>        <span class="token comment" spellcheck="true">%注意zeros生成的矩阵默认类型为double</span>
<span class="token keyword">for</span> <span class="token number">i</span><span class="token operator">=</span><span class="token number">1</span><span class="token operator">:</span>row
    <span class="token keyword">for</span> <span class="token number">j</span><span class="token operator">=</span><span class="token number">1</span><span class="token operator">:</span>col
        <span class="token function">res</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token function">index</span><span class="token punctuation">(</span><span class="token function">im_st</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token keyword">end</span>
<span class="token keyword">end</span>
res<span class="token operator">=</span><span class="token function">uint8</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span><span class="token punctuation">;</span>
figure<span class="token punctuation">;</span><span class="token function">imshow</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span><span class="token punctuation">;</span>figure<span class="token punctuation">;</span><span class="token function">imhist</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<h4 id="空域滤波"><a href="#空域滤波" class="headerlink" title="空域滤波"></a>空域滤波</h4><pre class=" language-matlab"><code class="language-matlab"><span class="token comment" spellcheck="true">% 模板处理,作用于像素邻域的处理方法</span>
<span class="token comment" spellcheck="true">% 模板，本质为矩阵方块，其数学含义是一种卷积运算</span>
<span class="token comment" spellcheck="true">% 卷积运算，图像的在模板区域里的每个像素的灰度值和模板对应的数值相乘，在把这些值求和</span>
针对卷积运算，有边界处理问题，有三种解决办法
<span class="token number">1</span>、丢失边缘信息
<span class="token number">2</span>、保留边缘信息
<span class="token number">3</span>、扩展图像，根据模板大小扩行扩列</code></pre>
<pre class=" language-matlab"><code class="language-matlab"><span class="token comment" spellcheck="true">% 平滑：模糊处理,去除图像中一些不重要的细节;减小噪声</span>
<span class="token comment" spellcheck="true">% 平滑滤波器(线性滤波器、非线性滤波器)</span>
<span class="token comment" spellcheck="true">% 线性滤波器(常用线性模板,均值滤波器)</span>
<span class="token comment" spellcheck="true">% 常用线性模板会给出了，用模板来做卷积运算</span>
如模板为 <span class="token operator">:</span>   <span class="token punctuation">(</span><span class="token number">1</span><span class="token operator">/</span><span class="token number">9</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token punctuation">[</span> <span class="token number">1</span> <span class="token punctuation">,</span> <span class="token number">1</span> <span class="token punctuation">,</span> <span class="token number">1</span>
                      <span class="token number">1</span> <span class="token punctuation">,</span> <span class="token number">1</span> <span class="token punctuation">,</span> <span class="token number">1</span>
                      <span class="token number">1</span> <span class="token punctuation">,</span> <span class="token number">1</span> <span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span> 
im<span class="token operator">=</span><span class="token function">imread</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
im<span class="token operator">=</span><span class="token function">im2double</span><span class="token punctuation">(</span>im<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">[</span>row<span class="token punctuation">,</span>col<span class="token punctuation">]</span><span class="token operator">=</span><span class="token function">size</span><span class="token punctuation">(</span>im<span class="token punctuation">)</span><span class="token punctuation">;</span>
res<span class="token operator">=</span>im<span class="token punctuation">;</span>                 <span class="token comment" spellcheck="true">%保留边缘信息</span>
<span class="token comment" spellcheck="true">% res=zeros(row,col);    丢失边缘信息</span>
<span class="token keyword">for</span> <span class="token number">i</span><span class="token operator">=</span><span class="token number">2</span><span class="token operator">:</span>row<span class="token number">-1</span>            <span class="token comment" spellcheck="true">% 设n为模板大小，当前模板的大小为3，及n=3</span>
    <span class="token keyword">for</span> <span class="token number">j</span><span class="token operator">=</span><span class="token number">2</span><span class="token operator">:</span>col<span class="token number">-1</span>        <span class="token comment" spellcheck="true">% 2和row-1的1的算法:起始点=(n+1)/2;终止点=(n-1)/2</span>
        <span class="token function">res</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token function">im</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">*</span><span class="token number">1</span><span class="token operator">+</span><span class="token function">im</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token operator">*</span><span class="token number">1</span><span class="token operator">+</span><span class="token function">im</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">*</span><span class="token number">1</span><span class="token operator">+</span><span class="token punctuation">...</span>
                 <span class="token function">im</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">*</span><span class="token number">1</span><span class="token operator">+</span><span class="token function">im</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token operator">*</span><span class="token number">1</span><span class="token operator">+</span><span class="token function">im</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">*</span><span class="token number">1</span><span class="token operator">+</span><span class="token punctuation">...</span>
                 <span class="token function">im</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">*</span><span class="token number">1</span><span class="token operator">+</span><span class="token function">im</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token operator">*</span><span class="token number">1</span><span class="token operator">+</span><span class="token function">im</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">*</span><span class="token number">1</span><span class="token punctuation">;</span>
        <span class="token function">res</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token function">res</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token operator">/</span><span class="token number">9</span><span class="token punctuation">;</span>
    <span class="token keyword">end</span>
<span class="token keyword">end</span>
figure<span class="token punctuation">;</span><span class="token function">imshow</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">%均值滤波器 模板就是上面演示的模板</span>
<span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span>
<span class="token comment" spellcheck="true">% 非线性滤波器</span>
<span class="token comment" spellcheck="true">% 统计排序滤波器(中值滤波器、最大值滤波器、最小值滤波器)</span>
演示中值滤波器<span class="token punctuation">(</span>用像素领域内的中值来代替中间像素值<span class="token punctuation">)</span>
im<span class="token operator">=</span><span class="token function">imread</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">[</span>row<span class="token punctuation">,</span>col<span class="token punctuation">]</span><span class="token operator">=</span><span class="token function">size</span><span class="token punctuation">(</span>im<span class="token punctuation">)</span><span class="token punctuation">;</span>
res<span class="token operator">=</span><span class="token function">zeros</span><span class="token punctuation">(</span>row<span class="token punctuation">,</span>col<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">for</span> <span class="token number">i</span><span class="token operator">=</span><span class="token number">2</span><span class="token operator">:</span>row<span class="token number">-1</span>
    <span class="token keyword">for</span> <span class="token number">j</span><span class="token operator">=</span><span class="token number">2</span><span class="token operator">:</span>col<span class="token number">-1</span>
        nums<span class="token operator">=</span><span class="token punctuation">[</span><span class="token function">im</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">im</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">im</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token punctuation">...</span>
              <span class="token function">im</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">im</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">im</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token punctuation">...</span>
              <span class="token function">im</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">im</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token function">im</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
        nums<span class="token operator">=</span><span class="token function">sort</span><span class="token punctuation">(</span>nums<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">res</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token function">nums</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">%res(i,j)=nums(1);    最小值滤波器</span>
        <span class="token comment" spellcheck="true">%res(i,j)=nums(9);    最大值滤波器</span>
    <span class="token keyword">end</span>
<span class="token keyword">end</span>
res<span class="token operator">=</span><span class="token function">uint8</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span><span class="token punctuation">;</span>
figure<span class="token punctuation">;</span><span class="token function">imshow</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
<pre class=" language-matlab"><code class="language-matlab"><span class="token comment" spellcheck="true">% 锐化：加强图像的细节边缘和轮廓，增强模糊的细节</span>
<span class="token comment" spellcheck="true">% 锐化滤波器 -- 水平方向的一阶锐化、垂直方向的一阶锐化、Roberts算子、Sobel算子、Priwitt算子、Laplacian算子、Wallis算子</span>
这些都是给定一个模板，让原图像和模板做卷积运算
在处理过程中，有些像素的灰度值会变为负的，处理办法如下：
<span class="token number">1</span>、给处理后的图片加上一个正整数，结果是获得类似浮雕效果
<span class="token number">2</span>、所有像素取绝对值，结果是获得对边缘的有效提取
</code></pre>
<h4 id="频域图像处理"><a href="#频域图像处理" class="headerlink" title="频域图像处理"></a>频域图像处理</h4><pre class=" language-matlab"><code class="language-matlab"><span class="token comment" spellcheck="true">% 频域的作用：简化运算（在频域的乘法运算在空域中为卷积运算）</span>
频域增强的步骤：
<span class="token number">1</span>、读入一张图片，并进行频域变化  （一般为傅里叶变化）
<span class="token number">2</span>、通过变化将频域移至中心
<span class="token number">3</span>、构造转移函数（核心为转移函数的构造，其余步骤均为相同的格式化写法）
<span class="token number">4</span>、转移函数和频域相乘
<span class="token number">5</span>、反频域变化
<span class="token number">6</span>、对图像的值取实部
<span class="token number">7</span>、输出图片
<span class="token comment" spellcheck="true">% 频域滤波分类（低通滤波器、高通滤波器、带通带阻滤波器）</span>
<span class="token comment" spellcheck="true">% 低通滤波器（理想低通滤波器、Butterworth低通滤波器、高斯低通滤波器）</span></code></pre>
<pre class=" language-matlab"><code class="language-matlab"><span class="token comment" spellcheck="true">% 理想低通滤波器（完整过程，剩余的只注重于转移函数的构造）</span>
im<span class="token operator">=</span><span class="token function">imread</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">[</span>row<span class="token punctuation">,</span>col<span class="token punctuation">]</span><span class="token operator">=</span><span class="token function">size</span><span class="token punctuation">(</span>im<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">% 步骤前两步</span>
s<span class="token operator">=</span><span class="token function">fft2</span><span class="token punctuation">(</span>im<span class="token punctuation">)</span><span class="token punctuation">;</span>
s<span class="token operator">=</span><span class="token function">fftshift</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">% 显示频谱</span>
figure<span class="token punctuation">;</span><span class="token function">imshow</span><span class="token punctuation">(</span><span class="token function">abs</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>          <span class="token comment" spellcheck="true">% s此时类型为double型，且值有负，因此取绝对值并加了一个[]</span>
figure<span class="token punctuation">;</span><span class="token function">imshow</span><span class="token punctuation">(</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token function">abs</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>     <span class="token comment" spellcheck="true">% 对频谱进行log运算后显示</span>
<span class="token comment" spellcheck="true">% 构造转移函数</span>
d0<span class="token operator">=</span><span class="token number">20</span><span class="token punctuation">;</span>
h<span class="token operator">=</span><span class="token function">zeros</span><span class="token punctuation">(</span>row<span class="token punctuation">,</span>col<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">for</span> <span class="token number">i</span><span class="token operator">=</span><span class="token number">1</span><span class="token operator">:</span>row
    <span class="token keyword">for</span> <span class="token number">j</span><span class="token operator">=</span><span class="token number">1</span><span class="token operator">:</span>col
        dis<span class="token operator">=</span><span class="token function">sqrt</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token operator">-</span>row<span class="token operator">/</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token operator">^</span><span class="token number">2</span><span class="token operator">+</span><span class="token punctuation">(</span><span class="token number">j</span><span class="token operator">-</span>col<span class="token operator">/</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token operator">^</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> dis<span class="token operator">&lt;</span>d0
            <span class="token function">h</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>
        <span class="token keyword">else</span>
            <span class="token function">h</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
        <span class="token keyword">end</span>
    <span class="token keyword">end</span>
<span class="token keyword">end</span>
s<span class="token operator">=</span>h<span class="token operator">.*</span>s<span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">% 反频域变化</span>
s<span class="token operator">=</span><span class="token function">ifftshift</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span><span class="token punctuation">;</span>
s<span class="token operator">=</span><span class="token function">ifft2</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">% 取实部</span>
s<span class="token operator">=</span><span class="token function">uint8</span><span class="token punctuation">(</span><span class="token function">real</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
figure<span class="token punctuation">;</span><span class="token function">imshow</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span>
<span class="token comment" spellcheck="true">% Butterworth低通滤波器</span>
<span class="token comment" spellcheck="true">% 构造转移函数</span>
d0<span class="token operator">=</span><span class="token number">20</span><span class="token punctuation">;</span>
n<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">;</span>
h<span class="token operator">=</span><span class="token function">zeros</span><span class="token punctuation">(</span>row<span class="token punctuation">,</span>col<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">for</span> <span class="token number">i</span><span class="token operator">=</span><span class="token number">1</span><span class="token operator">:</span>row
    <span class="token keyword">for</span> <span class="token number">j</span><span class="token operator">=</span><span class="token number">1</span><span class="token operator">:</span>col
        dis<span class="token operator">=</span><span class="token function">sqrt</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token operator">-</span>row<span class="token operator">/</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token operator">^</span><span class="token number">2</span><span class="token operator">+</span><span class="token punctuation">(</span><span class="token number">j</span><span class="token operator">-</span>col<span class="token operator">/</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token operator">^</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">h</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token number">1</span><span class="token operator">/</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token operator">+</span><span class="token punctuation">(</span>dis<span class="token operator">/</span>d0<span class="token punctuation">)</span><span class="token operator">^</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token operator">*</span>n<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">end</span>
<span class="token keyword">end</span>
s<span class="token operator">=</span>h<span class="token operator">.*</span>s<span class="token punctuation">;</span>
<span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span>
<span class="token comment" spellcheck="true">% 高斯低通滤波器</span>
<span class="token comment" spellcheck="true">% 构造转移函数</span>
d0<span class="token operator">=</span><span class="token number">20</span><span class="token punctuation">;</span>
h<span class="token operator">=</span><span class="token function">zeros</span><span class="token punctuation">(</span>row<span class="token punctuation">,</span>col<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">for</span> <span class="token number">i</span><span class="token operator">=</span><span class="token number">1</span><span class="token operator">:</span>row
    <span class="token keyword">for</span> <span class="token number">j</span><span class="token operator">=</span><span class="token number">1</span><span class="token operator">:</span>col
        dis<span class="token operator">=</span><span class="token function">sqrt</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token operator">-</span>row<span class="token operator">/</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token operator">^</span><span class="token number">2</span><span class="token operator">+</span><span class="token punctuation">(</span><span class="token number">j</span><span class="token operator">-</span>col<span class="token operator">/</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token operator">^</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">h</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token function">exp</span><span class="token punctuation">(</span><span class="token operator">-</span>dis<span class="token operator">^</span><span class="token number">2</span><span class="token operator">/</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token operator">*</span>d0<span class="token punctuation">)</span><span class="token operator">^</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">end</span>
<span class="token keyword">end</span>
s<span class="token operator">=</span>h<span class="token operator">.*</span>s<span class="token punctuation">;</span></code></pre>
<pre class=" language-matlab"><code class="language-matlab"><span class="token comment" spellcheck="true">% 高通滤波器（理想高通滤波器、Butterworth高通滤波器、高斯高通滤波器）</span>
高通滤波器和低通的区别为：高通让高频信息通过，低通让低频信息通过，因此，核心同样在与转移函数的构造上，可以直接取反低通滤波器来得到高通滤波器<span class="token punctuation">,</span>看下面的列子<span class="token operator">:</span>
<span class="token comment" spellcheck="true">% 高斯高通滤波器</span>
<span class="token comment" spellcheck="true">% 构造转移函数</span>
d0<span class="token operator">=</span><span class="token number">20</span><span class="token punctuation">;</span>
h<span class="token operator">=</span><span class="token function">zeros</span><span class="token punctuation">(</span>row<span class="token punctuation">,</span>col<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">for</span> <span class="token number">i</span><span class="token operator">=</span><span class="token number">1</span><span class="token operator">:</span>row
    <span class="token keyword">for</span> <span class="token number">j</span><span class="token operator">=</span><span class="token number">1</span><span class="token operator">:</span>col
        dis<span class="token operator">=</span><span class="token function">sqrt</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token operator">-</span>row<span class="token operator">/</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token operator">^</span><span class="token number">2</span><span class="token operator">+</span><span class="token punctuation">(</span><span class="token number">j</span><span class="token operator">-</span>col<span class="token operator">/</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token operator">^</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">h</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token function">exp</span><span class="token punctuation">(</span><span class="token operator">-</span>dis<span class="token operator">^</span><span class="token number">2</span><span class="token operator">/</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token operator">*</span>d0<span class="token punctuation">)</span><span class="token operator">^</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">end</span>
<span class="token keyword">end</span>
h<span class="token operator">=</span><span class="token number">1</span><span class="token operator">-</span>h<span class="token punctuation">;</span>             <span class="token comment" spellcheck="true">% 对低通滤波取反</span>
s<span class="token operator">=</span>h<span class="token operator">.*</span>s<span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">% 或者是按照高斯高通的公式来完成</span>
d0<span class="token operator">=</span><span class="token number">20</span><span class="token punctuation">;</span>
h<span class="token operator">=</span><span class="token function">zeros</span><span class="token punctuation">(</span>row<span class="token punctuation">,</span>col<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">for</span> <span class="token number">i</span><span class="token operator">=</span><span class="token number">1</span><span class="token operator">:</span>row
    <span class="token keyword">for</span> <span class="token number">j</span><span class="token operator">=</span><span class="token number">1</span><span class="token operator">:</span>col
        dis<span class="token operator">=</span><span class="token function">sqrt</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token operator">-</span>row<span class="token operator">/</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token operator">^</span><span class="token number">2</span><span class="token operator">+</span><span class="token punctuation">(</span><span class="token number">j</span><span class="token operator">-</span>col<span class="token operator">/</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token operator">^</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">h</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token number">1</span><span class="token operator">-</span><span class="token function">exp</span><span class="token punctuation">(</span><span class="token operator">-</span>dis<span class="token operator">^</span><span class="token number">2</span><span class="token operator">/</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token operator">*</span>d0<span class="token punctuation">)</span><span class="token operator">^</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>         <span class="token comment" spellcheck="true">% 按照公式的方式</span>
    <span class="token keyword">end</span>
<span class="token keyword">end</span>
s<span class="token operator">=</span>h<span class="token operator">.*</span>s<span class="token punctuation">;</span></code></pre>
<pre class=" language-matlab"><code class="language-matlab"><span class="token comment" spellcheck="true">% 带通带阻滤波器</span>
<span class="token comment" spellcheck="true">% 演示理想带阻和巴特沃斯带阻，带通取反即可</span>
<span class="token comment" spellcheck="true">% 构造转移函数</span>
d0<span class="token operator">=</span><span class="token number">20</span><span class="token punctuation">;</span>
w<span class="token operator">=</span><span class="token number">20</span><span class="token punctuation">;</span>                  <span class="token comment" spellcheck="true">% w表示带宽的大小</span>
h<span class="token operator">=</span><span class="token function">zeros</span><span class="token punctuation">(</span>row<span class="token punctuation">,</span>col<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">for</span> <span class="token number">i</span><span class="token operator">=</span><span class="token number">1</span><span class="token operator">:</span>row
    <span class="token keyword">for</span> <span class="token number">j</span><span class="token operator">=</span><span class="token number">1</span><span class="token operator">:</span>col
        dis<span class="token operator">=</span><span class="token function">sqrt</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token operator">-</span>row<span class="token operator">/</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token operator">^</span><span class="token number">2</span><span class="token operator">+</span><span class="token punctuation">(</span><span class="token number">j</span><span class="token operator">-</span>col<span class="token operator">/</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token operator">^</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> dis<span class="token operator">&lt;</span>d0<span class="token operator">-</span>w<span class="token operator">/</span><span class="token number">2</span>
            <span class="token function">h</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>
        <span class="token keyword">elseif</span> dis<span class="token operator">&lt;=</span>d0<span class="token operator">+</span>w<span class="token operator">/</span><span class="token number">2</span>
            <span class="token function">h</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
        <span class="token keyword">else</span>
            <span class="token function">h</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token punctuation">,</span><span class="token number">j</span><span class="token punctuation">)</span><span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>
        <span class="token keyword">end</span>

    <span class="token keyword">end</span>
<span class="token keyword">end</span>
<span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span>
d0<span class="token operator">=</span><span class="token number">20</span><span class="token punctuation">;</span>
w<span class="token operator">=</span><span class="token number">20</span><span class="token punctuation">;</span>
n<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">;</span>
h<span class="token operator">=</span><span class="token function">zeros</span><span class="token punctuation">(</span>row<span class="token punctuation">,</span>col<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">for</span> <span class="token number">i</span><span class="token operator">=</span><span class="token number">1</span><span class="token operator">:</span>row
    <span class="token keyword">for</span> <span class="token number">j</span><span class="token operator">=</span><span class="token number">1</span><span class="token operator">:</span>col
        dis<span class="token operator">=</span><span class="token function">sqrt</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token number">i</span><span class="token operator">-</span>row<span class="token operator">/</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token operator">^</span><span class="token number">2</span><span class="token operator">+</span><span class="token punctuation">(</span><span class="token number">j</span><span class="token operator">-</span>col<span class="token operator">/</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token operator">^</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        h<span class="token operator">=</span><span class="token number">1</span><span class="token operator">/</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token operator">+</span><span class="token punctuation">(</span><span class="token punctuation">(</span>dis<span class="token operator">*</span>w<span class="token punctuation">)</span><span class="token operator">/</span><span class="token punctuation">(</span>dis<span class="token operator">^</span><span class="token number">2</span><span class="token operator">-</span>d0<span class="token operator">^</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">^</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token operator">*</span>n<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">end</span>
<span class="token keyword">end</span>
</code></pre>
<h4 id="彩色图像处理"><a href="#彩色图像处理" class="headerlink" title="彩色图像处理"></a>彩色图像处理</h4><pre class=" language-matlab"><code class="language-matlab">彩色模型（彩色空间）：按照某种标准来指定颜色，为了便于硬件实现或便于对颜色的控制
彩色空间有：RGB，CMY和CMYK，HSI，YUV，YCbCr等等</code></pre>
<pre class=" language-matlab"><code class="language-matlab"><span class="token comment" spellcheck="true">% RGB  (***注意以下是针对彩色图像***)</span>
im<span class="token operator">=</span><span class="token function">imread</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">% RGB彩色图三原色的分解</span>
R<span class="token operator">=</span><span class="token function">im</span><span class="token punctuation">(</span><span class="token operator">:</span><span class="token punctuation">,</span><span class="token operator">:</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
G<span class="token operator">=</span><span class="token function">im</span><span class="token punctuation">(</span><span class="token operator">:</span><span class="token punctuation">,</span><span class="token operator">:</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
B<span class="token operator">=</span><span class="token function">im</span><span class="token punctuation">(</span><span class="token operator">:</span><span class="token punctuation">,</span><span class="token operator">:</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">% 由R、G、B分量合成彩色图片</span>
res<span class="token operator">=</span><span class="token function">cat</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span>R<span class="token punctuation">,</span>G<span class="token punctuation">,</span>B<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span>
<span class="token comment" spellcheck="true">% 判断读入的图像是否为彩色并作处理</span>
<span class="token keyword">if</span> <span class="token function">numel</span><span class="token punctuation">(</span><span class="token function">size</span><span class="token punctuation">(</span>im<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">&lt;</span><span class="token number">3</span>   <span class="token comment" spellcheck="true">% 或者  numel(size(im))==2 </span>
    <span class="token function">disp</span><span class="token punctuation">(</span><span class="token string">'你输入的图片不是彩色图片'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span><span class="token punctuation">;</span>       <span class="token comment" spellcheck="true">% 要有，直接退出程序，防止后续代码执行</span>
<span class="token keyword">end</span>
<span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span>
<span class="token comment" spellcheck="true">% 彩色空间的转换</span>
举例：RGB 转为 <span class="token function">YIQ</span> <span class="token punctuation">(</span>会给出转换的公式，根据公式来做<span class="token punctuation">)</span>
Y<span class="token operator">=</span><span class="token number">0.299</span><span class="token operator">*</span>R<span class="token operator">+</span><span class="token number">0.587</span><span class="token operator">*</span>G<span class="token operator">+</span><span class="token number">0.114</span><span class="token operator">*</span>B<span class="token punctuation">;</span>
I<span class="token operator">=</span><span class="token number">0.596</span><span class="token operator">*</span>R<span class="token number">-0.274</span><span class="token operator">*</span>G<span class="token number">-0.322</span><span class="token operator">*</span>B<span class="token punctuation">;</span>
Q<span class="token operator">=</span><span class="token number">0.211</span><span class="token operator">*</span>R<span class="token operator">+</span><span class="token number">0.523</span><span class="token operator">*</span>G<span class="token operator">+</span><span class="token number">0.312</span><span class="token operator">*</span>B<span class="token punctuation">;</span>
<span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span>
<span class="token comment" spellcheck="true">% 伪彩色图片处理（强度/密度分层技术、灰度-彩色变换法、频域滤波法）</span>
根据一定的规则将灰度图像转化为彩色图像或将单色图像变换成给定彩色分布的图像
<span class="token comment" spellcheck="true">% 强度/密度分层技术（要用到grayslice和jet函数）</span>
<span class="token comment" spellcheck="true">% 灰度-彩色变换法(对新建的R、G、B分量按照原图的灰度值分布来做空域相关处理，处理后合成为彩色图片)</span>
<span class="token comment" spellcheck="true">% 频域滤波法</span>
步骤：
<span class="token number">1</span>、对原图像做傅里叶变化
<span class="token number">2</span>、构造出不同或相同的转移函数
<span class="token number">3</span>、让这些转移函数和原图像的频谱相乘得到R，G，B三个灰度图片
<span class="token number">4</span>、对三个分量做反傅里叶变化
<span class="token number">5</span>、最后合成为彩色图片
<span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span>
<span class="token comment" spellcheck="true">% 全彩色图像处理</span>
分类：
<span class="token number">1</span>、对每个分量处理，最后三个分量合成为一张新的彩色图片     <span class="token comment" spellcheck="true">% 处理就是空域和频域中对灰度图像那些处理方法</span>
<span class="token number">2</span>、直接对彩色分量来处理
<span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span>
对彩色图像处理的一般步骤
<span class="token number">1</span>、读入一张彩色图片
<span class="token number">2</span>、分解三原色
<span class="token number">3</span>、（可选）根据需要将RGB转到其他彩色空间
<span class="token number">4</span>、对图像进行相应处理
<span class="token number">5</span>、（可选）将处理结果转回RGB空间
<span class="token number">6</span>、合并三个分量，输出图片
</code></pre>
<h2 id="边缘检测"><a href="#边缘检测" class="headerlink" title="边缘检测"></a>边缘检测</h2><pre class=" language-matlab"><code class="language-matlab"><span class="token comment" spellcheck="true">% 边缘提取</span>
步骤：
<span class="token number">1</span>、抑制噪声（低通、平滑、模糊、去噪）
<span class="token number">2</span>、增强边缘信息（高通、锐化）
<span class="token number">3</span>、定位边缘（边缘检测）
<span class="token number">4</span>、边缘连接</code></pre>
<pre class=" language-matlab"><code class="language-matlab"><span class="token comment" spellcheck="true">% 边缘检测（点检测、线检测、霍夫直线检测）</span>
<span class="token comment" spellcheck="true">% 点检测</span>
<span class="token number">1</span>、通过算子（给定的模板）对图像进行处理
<span class="token number">2</span>、设立阈值（单阈值或双阈值）
<span class="token number">3</span>、单阈值：按要求来弄，如提取边缘的图像的当前灰度值超过设定的阈值，就赋值为<span class="token number">255</span>，反之，赋值为<span class="token number">0</span>
<span class="token number">4</span>、多阈值：一样，按照要求弄
<span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span>
<span class="token comment" spellcheck="true">% 线检测</span>
给定一些方向模板（上、下、<span class="token number">45</span>度斜线、<span class="token operator">-</span><span class="token number">45</span>度斜线），用方向模板和经算子处理后的图片做卷积运算，得到的结果值最小的就是近似代表这个边缘的方向
<span class="token comment" spellcheck="true">% 霍夫直线检测（难，略）</span>
<span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span>
<span class="token comment" spellcheck="true">% 边缘连接</span>
在边缘检测后，通过梯度算子的响应强度和梯度方向确定两个点是否属于同一条边，是，就将两点之间的灰度赋值为<span class="token number">255</span>（一个数）
<span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span><span class="token operator">-</span>
<span class="token comment" spellcheck="true">% Canny算子（边缘提取目前最优算子，步骤较复杂，略）</span>
</code></pre>
<h2 id="图像复原"><a href="#图像复原" class="headerlink" title="图像复原"></a>图像复原</h2><pre class=" language-matlab"><code class="language-matlab">对图像的复原方法有空域处理、频域处理、其他方法
对于空域的，考试不会超过图像增强中空域处理的办法，对于频域同理
故，图像复原就水掉了
</code></pre>
<h2 id="结语"><a href="#结语" class="headerlink" title="结语"></a>结语</h2><p>此总结当作我学习图像的记录，花了两三天完成吧，期间还帮舍友补课。经过图像的学习，对于图片有了更多的理解，以及对于在计算机中坐标系的认识更加深入，后续如果我对于图像处理有更多的兴趣，我应该会在学一学计算机图形学吧。</p>

  </article>

  
      
    <div class="nexmoe-post-copyright">
        <strong>本文作者：</strong>塔纳托斯<br>
        <strong>本文链接：</strong><a href="http://hai-chao.gitee.io/blog/2021/06/26/19.tu-xiang-qi-mo-zhi-nan/" title="http:&#x2F;&#x2F;hai-chao.gitee.io&#x2F;blog&#x2F;2021&#x2F;06&#x2F;26&#x2F;19.tu-xiang-qi-mo-zhi-nan&#x2F;" target="_blank" rel="noopener">http:&#x2F;&#x2F;hai-chao.gitee.io&#x2F;blog&#x2F;2021&#x2F;06&#x2F;26&#x2F;19.tu-xiang-qi-mo-zhi-nan&#x2F;</a><br>
        
            <strong>版权声明：</strong>本文采用 <a href="https://creativecommons.org/licenses/by-nc-sa/3.0/cn/deed.zh" target="_blank">CC BY-NC-SA 3.0 CN</a> 协议进行许可
        
    </div>


  
  
  <div class="nexmoe-post-meta nexmoe-rainbow">
    
    
        <a class="nexmoefont icon-tag-fill -none-link" href="/blog/tags/%E6%80%BB%E7%BB%93/" rel="tag">总结</a>
    
</div>

  
      <div class="nexmoe-post-footer">
          
      </div>
  
</div>
            <div class="nexmoe-post-right">
              <div class="nexmoe-fixed">
                  <div class="nexmoe-tool"> 
                    
                      
                        
                          
                          
                              <button class="mdui-fab catalog" style="overflow:unset;">
                                  <i class="nexmoefont icon-i-catalog"></i>
                                  <div class="nexmoe-toc">
                                      <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#Matlab%E5%9B%BE%E5%83%8F%E5%9F%BA%E7%A1%80"><span class="toc-number">1.</span> <span class="toc-text">Matlab图像基础</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9B%BE%E5%83%8F%E5%A2%9E%E5%BC%BA"><span class="toc-number">2.</span> <span class="toc-text">图像增强</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%A9%BA%E5%9F%9F%E7%82%B9%E8%BF%90%E7%AE%97"><span class="toc-number">2.0.1.</span> <span class="toc-text">空域点运算</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%A9%BA%E5%9F%9F%E6%BB%A4%E6%B3%A2"><span class="toc-number">2.0.2.</span> <span class="toc-text">空域滤波</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%A2%91%E5%9F%9F%E5%9B%BE%E5%83%8F%E5%A4%84%E7%90%86"><span class="toc-number">2.0.3.</span> <span class="toc-text">频域图像处理</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%BD%A9%E8%89%B2%E5%9B%BE%E5%83%8F%E5%A4%84%E7%90%86"><span class="toc-number">2.0.4.</span> <span class="toc-text">彩色图像处理</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%BE%B9%E7%BC%98%E6%A3%80%E6%B5%8B"><span class="toc-number">3.</span> <span class="toc-text">边缘检测</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9B%BE%E5%83%8F%E5%A4%8D%E5%8E%9F"><span class="toc-number">4.</span> <span class="toc-text">图像复原</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BB%93%E8%AF%AD"><span class="toc-number">5.</span> <span class="toc-text">结语</span></a></li></ol>
                                  </div>
                              </button>
                          
                          
                      
                    
                      <a href="#nexmoe-content" class="toc-link" aria-label="回到顶部" title="top"><button class="mdui-fab mdui-ripple"><i class="nexmoefont icon-caret-top"></i></button></a>
                  </div>
              </div>
            </div>
        </div>
    </div>
    
    <script src="https://cdn.jsdelivr.net/combine/npm/lazysizes@5.1.0/lazysizes.min.js,npm/mdui@0.4.3/dist/js/mdui.min.js?v=1"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js"></script>

 

<script async src="/blog/js/app.js?v=1679297521152"></script>



<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.8.1/dist/js/jquery.justifiedGallery.min.js"></script>
<script>
	$(".justified-gallery").justifiedGallery({
		rowHeight: 160,
		margins: 10,
	});
</script>


    





</body>

</html>
